package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BaseJdbc {
    private static final String Driver = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    public static Connection getConnection() {
        Connection conn;
        try {
            Class.forName(Driver); // 用反射加载数据库驱动
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
            return conn; // 取得数据库的连接
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void close(Connection conn) throws SQLException { // 关闭数据库
        if (conn != null) {
            conn.close(); // 数据库关闭
        }
    }
}
